package com.benchmark.springboot.controller;

import com.benchmark.springboot.component.RestTemplateClient;
import com.power.common.model.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.util.HashMap;

/**
 * http请求用例
 *
 * @author yu 2018/12/21.
 */
@RestController
public class HttpTestController {


    @Resource
    private RestTemplateClient restTemplateClient;

    /**
     * https方式访问
     * @apiNote 使用TestTemplate访问https网站
     * @return
     */
    @GetMapping("testHttps")
    public CommonResult<String> testHttps() {
        String url = "https://www.baidu.com/";
        String result = restTemplateClient.getForObject(url, String.class);
        return CommonResult.ok().setResult(result);
    }

    /**
     * 访问内部接口
     *
     * @return
     * @throws Exception
     */
    @GetMapping("testHttp")
    public CommonResult<String> testHttp() throws Exception {
        InetAddress addr = InetAddress.getLocalHost();
        String ip = addr.getHostAddress().toString();
        String url = String.format("http://%s:8080/", ip);
        String result = restTemplateClient.getForObject(url, String.class);
        return CommonResult.ok().setResult(result);
    }

    /**
     * 访问k8s service
     *
     * @param svc kubernetes service|springboot-docker-svc
     * @return
     */
    @GetMapping(value = "svc/{svc}")
    public CommonResult<String> testService(@PathVariable String svc) {
        String rest = restTemplateClient.postJson(svc, new HashMap<>());
        return CommonResult.ok().setResult(rest);
    }
}
